package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.OrderMemberService_zh;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/orderMember")
public class OrderMemberController_zh {
    @Reference
    private OrderMemberService_zh orderMemberService_zh;
    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Member member, Integer[] setmealIds){
        try{
            member.setOrderType(Order.ORDERTYPE_TELEPHONE);
            member.setOrderStatus(Order.ORDERSTATUS_NO);
            orderMemberService_zh.add(member,setmealIds);
            return new Result(true, MessageConstant.ADD_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody Map map){
        return orderMemberService_zh.findPage(map);
    }

    //根据id查询检查组
    @RequestMapping("/findById")
    public Result findById(Integer orderId){
        try{
            Member member = orderMemberService_zh.findById(orderId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try{
            orderMemberService_zh.edit(member);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //查询所有检查组
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Member> list = orderMemberService_zh.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    //删除
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id){
        try{
            orderMemberService_zh.delete(id);//发送请求
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (Exception e){
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, message);
        }
    }

    //更改到诊状态
    @RequestMapping("/changStatus")
    public Result changStatus(@RequestBody Map map){
        try{
            orderMemberService_zh.changStatus(map);
            return new Result(true, MessageConstant.CHANGE_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.CHANGE_ORDER_FAIL);
        }
    }



    @RequestMapping("/findSetmealIdsByOrderId")
    public Result findSetmealIdsByOrderId(Integer orderId) {
        try {
            List<Integer> list = orderMemberService_zh.findSetmealIdsByOrderId(orderId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
